{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 解读  `SourceMap`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## `SourceMapNode`\n",
    "\n",
    "```c++\n",
    "/*!\n",
    " * \\brief A mapping from a unique source name to source fragment.\n",
    " */\n",
    "class SourceMap;\n",
    "/*!\n",
    " * \\brief Stores locations in frontend source that generated a node.\n",
    " */\n",
    "class SourceMapNode : public Object {\n",
    " public:\n",
    "  /*! \\brief The source mapping. */\n",
    "  Map<SourceName, Source> source_map;\n",
    "\n",
    "  // override attr visitor\n",
    "  void VisitAttrs(AttrVisitor* v) { v->Visit(\"source_map\", &source_map); }\n",
    "\n",
    "  bool SEqualReduce(const SourceMapNode* other, SEqualReducer equal) const {\n",
    "    return equal(source_map, other->source_map);\n",
    "  }\n",
    "\n",
    "  static constexpr const char* _type_key = \"SourceMap\";\n",
    "  TVM_DECLARE_FINAL_OBJECT_INFO(SourceMapNode, Object);\n",
    "};\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`SourceMapNode` 继承自 `Object` 类，它表示源代码映射。该类具有以下成员变量和成员函数：\n",
    "\n",
    "- `source_map`：一个`Map<SourceName, Source>`类型的成员变量，表示源名称到源代码片段的映射。\n",
    "- `VisitAttrs(AttrVisitor* v)`：一个成员函数，用于访问属性。它接受一个指向`AttrVisitor`对象的指针作为参数，并调用`v->Visit(\"source_map\", &source_map)`来访问`source_map`属性。\n",
    "- `SEqualReduce(const SourceMapNode* other, SEqualReducer equal) const`：一个成员函数，用于比较两个`SourceMapNode`对象是否相等。它接受一个指向另一个`SourceMapNode`对象的指针和一个`SEqualReducer`对象作为参数，并使用`equal`函数比较两个对象的`source_map`属性是否相等。\n",
    "- `_type_key`：一个静态常量字符指针，表示该类型的键。\n",
    "- `TVM_DECLARE_FINAL_OBJECT_INFO(SourceMapNode, Object)`：一个宏定义，用于声明`SourceMapNode`对象的最终信息。\n",
    "\n",
    "总体来说，这段代码定义了一个表示源代码映射的类，并提供了相应的属性访问和相等性比较功能。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## `SourceMap`\n",
    "\n",
    "```c++\n",
    "\n",
    "class SourceMap : public ObjectRef {\n",
    " public:\n",
    "  explicit SourceMap(Map<SourceName, Source> source_map);\n",
    "\n",
    "  explicit SourceMap(std::initializer_list<std::pair<SourceName, Source>> source_map)\n",
    "      : SourceMap(Map<SourceName, Source>(source_map)) {}\n",
    "\n",
    "  SourceMap() : SourceMap(Map<SourceName, Source>()) {}\n",
    "\n",
    "  void Add(const Source& source);\n",
    "\n",
    "  SourceMapNode* operator->() {\n",
    "    ICHECK(get() != nullptr);\n",
    "    return static_cast<SourceMapNode*>(get_mutable());\n",
    "  }\n",
    "\n",
    "  TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(SourceMap, ObjectRef, SourceMapNode);\n",
    "};\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`SourceMap` 类继承自 `ObjectRef` 类。该类具有以下成员函数：\n",
    "\n",
    "- `explicit SourceMap(Map<SourceName, Source> source_map)`：一个构造函数，用于创建一个`SourceMap`对象。它接受一个`Map<SourceName, Source>`类型的参数`source_map`，表示源名称到源代码片段的映射。\n",
    "- `explicit SourceMap(std::initializer_list<std::pair<SourceName, Source>> source_map)`：另一个构造函数，用于创建一个`SourceMap`对象。它接受一个`std::initializer_list<std::pair<SourceName, Source>>`类型的参数`source_map`，表示源名称到源代码片段的映射。这个构造函数会将传入的初始化列表转换为`Map<SourceName, Source>`类型，并调用第一个构造函数进行创建。\n",
    "- `SourceMap()`：一个默认构造函数，用于创建一个空的`SourceMap`对象。它会调用第二个构造函数，传入一个空的初始化列表。\n",
    "- `Add(const Source& source)`：一个成员函数，用于向`SourceMap`中添加一个源代码片段。它接受一个指向`Source`对象的常量引用作为参数。\n",
    "- `operator->()`：一个重载的箭头运算符，用于获取指向`SourceMapNode`对象的指针。它首先检查当前对象是否为空，然后将当前对象转换为可变指针，并将其转换为`SourceMapNode*`类型。\n",
    "- `TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(SourceMap, ObjectRef, SourceMapNode)`：一个宏定义，用于定义`SourceMap`对象的非空引用方法。\n",
    "\n",
    "总体来说，这段代码定义了一个用于表示源代码映射的类，并提供了相应的构造函数、添加源代码片段和访问映射节点的功能。"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
